home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / JPasswordField.java < prev    next >
Text File  |  1998-06-30  |  8KB  |  234 lines

  1. /*
  2.  * @(#)JPasswordField.java    1.22 98/04/09
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing;
  21.  
  22. import com.sun.java.swing.text.*;
  23. import com.sun.java.swing.plaf.*;
  24. import com.sun.java.accessibility.*;
  25.  
  26. /**
  27.  * JPasswordField is a lightweight component that allows the editing 
  28.  * of a single line of text where the view indicates something was
  29.  * typed, but does not show the original characters. It is intended 
  30.  * to be source-compatible with java.awt.TextField used with echoChar 
  31.  * set.  It is provided seperately to make it easier to safely change 
  32.  * the ui for the JTextField without affecting password entries.
  33.  * <p>
  34.  * For the keyboard keys used by this component in the standard Look and
  35.  * Feel (L&F) renditions, see the
  36.  * <a href="doc-files/Key-Index.html#JPasswordField">JPasswordField</a>
  37.  * key assignments.
  38.  * <p>
  39.  * Warning: serialized objects of this class will not be compatible with
  40.  * future swing releases.  The current serialization support is appropriate 
  41.  * for short term storage or RMI between Swing1.0 applications.  It will
  42.  * not be possible to load serialized Swing1.0 objects with future releases
  43.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  44.  * baseline for the serialized form of Swing objects.
  45.  *
  46.  * @beaninfo
  47.  *  attribute: isContainer false
  48.  *
  49.  * @author  Timothy Prinzing
  50.  * @version 1.22, 04/09/98
  51.  */
  52. public class JPasswordField extends JTextField {
  53.  
  54.     /**
  55.      * Constructs a new JPasswordField, with a default document, null starting
  56.      * text string, and 0 column width.
  57.      */
  58.     public JPasswordField() {
  59.         this(null,null,0);
  60.     }
  61.  
  62.     /**
  63.      * Constructs a new JPasswordField initialized with the specified text.
  64.      * The document model is set to the default, and the number of columns to 0.
  65.      *
  66.      * @param text the text to be displayed, null if none
  67.      */
  68.     public JPasswordField(String text) {
  69.         this(null, text, 0);
  70.     }
  71.  
  72.     /**
  73.      * Constructs a new empty JPasswordField with the specified
  74.      * number of columns.  A default model is created, and the initial string
  75.      * is set to null.
  76.      *
  77.      * @param columns the number of columns >= 0
  78.      */ 
  79.     public JPasswordField(int columns) {
  80.         this(null, null, columns);
  81.     }
  82.  
  83.     /**
  84.      * Constructs a new JPasswordField initialized with the specified text
  85.      * and columns.  The document model is set to the default.
  86.      *
  87.      * @param text the text to be displayed, null if none
  88.      * @param columns the number of columns >= 0
  89.      */
  90.     public JPasswordField(String text, int columns) {
  91.         this(null, text, columns);
  92.     }
  93.  
  94.     /**
  95.      * Constructs a new JPasswordField that uses the given text storage
  96.      * model and the given number of columns.  This is the constructor
  97.      * through which the other constructors feed.  The echo character is
  98.      * set to '*'.  If the document model is null, a default one will be
  99.      * created.
  100.      *
  101.      * @param doc  the text storage to use
  102.      * @param txt the text to be displayed, null if none
  103.      * @param columns  the number of columns to use to calculate 
  104.      *   the preferred width >= 0.  If columns is set to zero, the
  105.      *   preferred width will be whatever naturally results from
  106.      *   the component implementation.
  107.      */
  108.     public JPasswordField(Document doc, String txt, int columns) {
  109.         super(doc, txt, columns);
  110.         echoChar = '*';
  111.     }
  112.  
  113.     /**
  114.      * Returns the name of the L&F class that renders this component.
  115.      *
  116.      * @return "PasswordFieldUI"
  117.      * @see JComponent#getUIClassID
  118.      * @see UIDefaults#getUI
  119.      */
  120.     public String getUIClassID() {
  121.         return "PasswordFieldUI";
  122.     }
  123.  
  124.  
  125.     /**
  126.      * Returns the character to be used for echoing.  The default is '*'.
  127.      *
  128.      * @return the echo character, 0 if unset
  129.      * @see #setEchoChar
  130.      * @see #echoCharIsSet
  131.      */
  132.     public char getEchoChar() {
  133.         return echoChar;
  134.     }
  135.  
  136.     /**
  137.      * Sets the echo character for this JPasswordField.  Note 
  138.      * that this is largely a suggestion to the view as the
  139.      * view that gets installed can use whatever graphic techniques
  140.      * it desires to represent the field.  Setting a value of 0 unsets
  141.      * the echo character.
  142.      *
  143.      * @param c the echo character to display
  144.      * @see #echoCharIsSet
  145.      * @see #getEchoChar
  146.      * @beaninfo
  147.      * description: character to display in place of the real characters
  148.      */
  149.     public void setEchoChar(char c) {
  150.         echoChar = c;
  151.     }
  152.  
  153.     /**
  154.      * Returns true if this JPasswordField has a character set for
  155.      * echoing.  A character is considered to be set if the echo character
  156.      * is not 0.
  157.      *
  158.      * @return true if a character is set for echoing
  159.      * @see #setEchoChar
  160.      * @see #getEchoChar
  161.      */
  162.     public boolean echoCharIsSet() {
  163.         return echoChar != 0;
  164.     }
  165.  
  166.     // --- JTextComponent methods ----------------------------------
  167.  
  168.     /**
  169.      * Normally transfers the currently selected range in the associated
  170.      * text model to the system clipboard, removing the contents
  171.      * from the model.  This is not a good thing for a password field
  172.      * and is reimplemented to simply beep.
  173.      */
  174.     public void cut() {
  175.     getToolkit().beep();
  176.     }
  177.  
  178.     /**
  179.      * Normally transfers the currently selected range in the associated
  180.      * text model to the system clipboard, leaving the contents
  181.      * in the text model.  This is not a good thing for a password field
  182.      * and is reimplemented to simply beep.
  183.      */
  184.     public void copy() {
  185.     getToolkit().beep();
  186.     }
  187.  
  188.     // --- variables -----------------------------------------------
  189.  
  190.     private char echoChar;
  191.  
  192. /////////////////
  193. // Accessibility support
  194. ////////////////
  195.  
  196.  
  197.     /**
  198.      * Gets the AccessibleContext associated with this JPasswordField.
  199.      * A new context is created as necessary.
  200.      *
  201.      * @return the AccessibleContext of this JPasswordField
  202.      */
  203.     public AccessibleContext getAccessibleContext() {
  204.         if (accessibleContext == null) {
  205.             accessibleContext = new AccessibleJPasswordField();
  206.         }
  207.         return accessibleContext;
  208.     }
  209.  
  210.     /**
  211.      * The class used to obtain the accessible role for this object.
  212.      * <p>
  213.      * Warning: serialized objects of this class will not be compatible with
  214.      * future swing releases.  The current serialization support is appropriate
  215.      * for short term storage or RMI between Swing1.0 applications.  It will
  216.      * not be possible to load serialized Swing1.0 objects with future releases
  217.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  218.      * baseline for the serialized form of Swing objects.
  219.      */
  220.     protected class AccessibleJPasswordField extends AccessibleJTextField {
  221.  
  222.         /**
  223.          * Gets the role of this object.
  224.          *
  225.          * @return an instance of AccessibleRole describing the role of the
  226.          *   object (AccessibleRole.PASSWORD_TEXT)
  227.          * @see AccessibleRole
  228.          */
  229.         public AccessibleRole getAccessibleRole() {
  230.             return AccessibleRole.PASSWORD_TEXT;
  231.         }
  232.     }
  233. }
  234.